RequireVersion ("0.9920060426");

_idx = "`HYPHY_LIB_DIRECTORY`TemplateBatchFiles`DIRECTORY_SEPARATOR`Utility`DIRECTORY_SEPARATOR`GrabBag.bf";
ExecuteCommands ("#include\"`_idx`\";");

_seqCount 		= Columns(_DATAPANEL_SELECTED_SEQUENCES_);
_selFilters		= Columns(_DATAPANEL_SELECTED_FILTERS_);

if (_seqCount == 0 || _selFilters > 1 || _seqCount == _DATAPANEL_DATAFILTER_.species)
{
	fprintf (stdout, "ERROR: This plug in-needs one selected data filter and some (but not all) selected sequences\n");
	return 0;
}

if (_selFilters == 1)
{
	_baseName = _DATAPANEL_SELECTED_FILTERS_[0];
}
else
{
	_baseName = _DATAPANEL_DATASET_NAME_;
}

_sequenceMapping = {};

for (_idx = 0; _idx < _seqCount; _idx = _idx + 1)
{
	_sequenceMapping[_DATAPANEL_SELECTED_SEQUENCES_[_idx]] = 1;
}

_unitSize 	= _DATAPANEL_UNIT_SIZE_[0];
_exclusions = _DATAPANEL_EXCLUSIONS_[0];

DataSetFilter _subset1 = CreateFilter (_DATAPANEL_DATAFILTER_,_unitSize,"",_sequenceMapping[speciesIndex] == 1,_exclusions);
DataSetFilter _subset2 = CreateFilter (_DATAPANEL_DATAFILTER_,_unitSize,"",_sequenceMapping[speciesIndex] == 0,_exclusions);
_svl = VERBOSITY_LEVEL;
VERBOSITY_LEVEL = -1;

_siteTypes = {};

_siteTypes["Fixed and same in both subsets"] = {};
_siteTypes["Fixed and different in both subsets"] = {};
_siteTypes["Fixed in selected sequences only"] = {};
_siteTypes["Fixed in excluded sequences only"] = {};
_siteTypes["Polymorphic in both subsets"] = {};

for (_idx = 0; _idx < _DATAPANEL_DATAFILTER_.sites; _idx = _idx + 1)
{
	if (_unitSize > 1)
	{
		_siteSpec = Format (_idx*_unitSize,20,0) + "-" + Format ((1+_idx)*_unitSize-1,20,0)
	}
	else
	{
		_siteSpec = Format (_idx,20,0);
	}
	DataSetFilter _aSite = CreateFilter (_subset1, _unitSize, _siteSpec,"",_exclusions);
	HarvestFrequencies     (_freqs1, _aSite, _unitSize, _unitSize, 0);
	DataSetFilter _aSite = CreateFilter (_subset2, _unitSize, _siteSpec,"",_exclusions);
	HarvestFrequencies     (_freqs2, _aSite, _unitSize, _unitSize, 0);
	
	_f = Abs (_freqs1-_freqs2);
	if (_f == Sqrt(2))
	{
		(_siteTypes["Fixed and different in both subsets"])[Abs(_siteTypes["Fixed and different in both subsets"])] = _idx;
	}
	else
	{
		if (_f == 0)
		{
			(_siteTypes["Fixed and same in both subsets"])[Abs(_siteTypes["Fixed and same in both subsets"])] = _idx;
		}
		else
		{
			if (Abs (_freqs1) == 1)
			{
				(_siteTypes["Fixed in selected sequences only"])[Abs(_siteTypes["Fixed in selected sequences only"])] = _idx;
		
			}
			else
			{
				if (Abs(_freqs2) == 1)
				{
					(_siteTypes["Fixed in excluded sequences only"])[Abs(_siteTypes["Fixed in excluded sequences only"])] = _idx;
				}
				else
				{
					(_siteTypes["Polymorphic in both subsets"])[Abs(_siteTypes["Polymorphic in both subsets"])] = _idx;
				}
			}
		}
	}
}

VERBOSITY_LEVEL = _svl;
_countMatrix	= Abs (_siteTypes); 
_idx			= avlToLabels (_siteTypes,"Kind",";");
_labels			= {{"Count","`_idx`"}};
_types			= Rows (_siteTypes);
_plotMx			= {Columns(_types),1};

_DATAPANEL_RETURNED_FILTERS_ = {};

for (_idx = 0; _idx < Columns (_types); _idx = _idx + 1)
{
	_plotMx [_idx] = Abs(_siteTypes[_types[_idx]]);
	
	_f = {};
	_f ["SITES"] = avlToRow (_siteTypes[_types[_idx]],_DATAPANEL_DATAFILTER_.site_map,_unitSize);
	if (Columns(_f["SITES"]))
	{
		_DATAPANEL_RETURNED_FILTERS_ [_baseName+_types[_idx]] = _f;
	}
	/*_f = avlToString (_siteTypes[_types[_idx]],",");
	fprintf (stdout, "\n", _types[_idx], "\n", _f ,"\n");*/
}

OpenWindow (CHARTWINDOW,{{"Site type comparison"}
		{"_labels"}
		{"_plotMx"}
		{"Pie Chart"}
		{"Count"}
		{"Count"}
		{""}
		{""}
		{""}
		{"1"}
		{""}
		{"-1;-1"}
		{"10;1.309;0.785398"}
		{"Times:12:0;Times:10:0;Times:12:2"}
		{"0;0;13816530;16777215;0;0;6579300;11842740;13158600;14474460;0;3947580;16777215;15670812;6845928;16771158;2984993;9199669;7018159;1460610;16748822;11184810;14173291"}
		{"16,0,0"}
		},
		"700;700;50;50");






